﻿using System;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Threading;
using DevExpress.XtraReports.UI;
using prjKQXS_DLL;

namespace prjKQXS_UI.Reports
{
    public partial class frmDaiLyTheChap : DevExpress.XtraEditors.XtraForm
    {
        /// <summary>
        /// False là đại lý cấp 1, True là đại lý cấp 2
        /// </summary>
        private int CapBac;

        #region frmDaiLyTheChap EVENTS---------------------------------------------------------
        public frmDaiLyTheChap(int Cap)
        {
            InitializeComponent();
            this.CapBac = Cap;
        }

        private void frmDaiLyTheChap_Load(object sender, EventArgs e)
        {
            this.Text = "Danh sách đại lý thế chấp cấp ".ToUpper();
            this.Text += CapBac == 1 ? "I" : "II";
            Control.CheckForIllegalCrossThreadCalls = false;
            marqueeProgressBarControl1.Properties.Stopped = true;
            loadDonVi();

            if (CapBac==1) cbbDLChuaRutTC.Enabled = false;

            deTuNgay.DateTime = DateTime.Now;
            deDenNgay.DateTime = DateTime.Now;

            sbtnChapNhan.Focus();
        }
        #endregion---------------------------------------------------------------------------------

        #region CONTROL EVENTS-----------------------------------------------------------------------
        private void deTuNgay_EditValueChanged(object sender, EventArgs e)
        {
            //cbbThang.SelectedIndex = deTuNgay.DateTime.Month - 1;
        }

        private void deDenNgay_EditValueChanged(object sender, EventArgs e)
        {
            //cbbDenThang.SelectedIndex = deDenNgay.DateTime.Month - 1;
            //cbbNam.Text = deDenNgay.DateTime.Year.ToString();
        }

        private void sbtnChapnhan_Click(object sender, EventArgs e)
        {
            marqueeProgressBarControl1.Properties.Stopped = false;
            Thread t = new Thread(new ThreadStart(inbaocao));
            t.SetApartmentState(ApartmentState.STA);
            t.Start();
        }

        private void sbtnBoqua_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion----------------------------------------------------------------------------------

        #region SUB AND FUNCTION--------------------------------------------------------------------
        SqlConnection conn = new SqlConnection(BienTc.connString);

        private void loadDonVi()
        {
            ucDonVi1.lueDonVi.Properties.DataSource = clsCommon.load_lueDonVi();

            if (BienTc.isQuanHuyen || BienTc.isThanhPho || !clsCommon.hasPermission(BienTc.UserID, (int)BienTc.eFunction.L_DonVi))
            {
                ucDonVi1.lueDonVi.ItemIndex = ucDonVi1.lueDonVi.Properties.GetDataSourceRowIndex("Ma_DT", BienTc.ma_huyen == "" ? "TC" : BienTc.ma_huyen);
                if (BienTc.isQuanHuyen || BienTc.isThanhPho) ucDonVi1.lueDonVi.Properties.ReadOnly = true;
            }
            else ucDonVi1.lueDonVi.ItemIndex = 0;
        }

        DateTime TuNgay, DenNgay;

        private void inbaocao()
        {
            TuNgay = deTuNgay.DateTime;
            DenNgay = deDenNgay.DateTime;

            if (TuNgay > DenNgay)
            {
                clsCommon.MsgBox("Khoảng thời gian nhập vào không hợp lệ. Vui lòng nhập lại", 5);
                deTuNgay.Focus();
            }
            else
            {
                if (CapBac == 1)
                {
                    prjKQXS_Report.DSDaiLyCap1 rpt = new prjKQXS_Report.DSDaiLyCap1(TuNgay, DenNgay);
                    rpt.ShowRibbonPreviewDialog();
                }
                else
                {
                    prjKQXS_Report.rptDSDaiLyTheChap_1Ky rpt;
                    if (cbbDLChuaRutTC.Checked) rpt = new prjKQXS_Report.rptDSDaiLyTheChap_1Ky(TuNgay, DenNgay, false);
                    else rpt = new prjKQXS_Report.rptDSDaiLyTheChap_1Ky(TuNgay, DenNgay);
                    rpt.ShowRibbonPreviewDialog();
                }
            }
            marqueeProgressBarControl1.Properties.Stopped = true;
        }
        #endregion-----------------------------------------------------------------------------------
    }
}